package com.be.beadmin.service.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.service.dto.input.FactoryCalendarItemDto;
import com.be.beadmin.service.dto.output.FactoryCalendarItemEx;
import com.be.beadmin.service.service.FactoryCalendarItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author chenwei
* @date 2023-02-22
**/
@RestController
public class FactoryCalendarItemController {

    @Autowired
    private  FactoryCalendarItemService factoryCalendarItemService;


    @PostMapping(value = "/service/getFactoryCalendarItemByPage")
    public ResultModel getFactoryCalendarItemByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(factoryCalendarItemService.getFactoryCalendarItemByPage(pageEntity)) ;
    }

    @PostMapping("/service/addFactoryCalendarItem")
    public ResultModel addFactoryCalendarItem(@RequestBody FactoryCalendarItemDto factoryCalendarItemDto){
//        factoryCalendarItemService.addFactoryCalendarItem(factoryCalendarItemDto);
        return ResultModel.success(factoryCalendarItemService.addFactoryCalendarItem(factoryCalendarItemDto));
    }

    @PostMapping("/service/delFactoryCalendarItem")
    public ResultModel delFactoryCalendarItem(@RequestBody FactoryCalendarItemDto factoryCalendarItemDto){
        factoryCalendarItemService.delFactoryCalendarItem(factoryCalendarItemDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/service/updateFactoryCalendarItem")
    public ResultModel updateFactoryCalendarItem(@RequestBody FactoryCalendarItemDto factoryCalendarItemDto){
        factoryCalendarItemService.updateFactoryCalendarItem(factoryCalendarItemDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/service/getRangeItems")
    public ResultModel getRangeItems(@RequestBody FactoryCalendarItemDto factoryCalendarItemDto){
        factoryCalendarItemService.findDays(factoryCalendarItemDto.getFactoryCalendarId(), factoryCalendarItemDto.getDate());
        return ResultModel.success(factoryCalendarItemService.findDays(factoryCalendarItemDto.getFactoryCalendarId(), factoryCalendarItemDto.getDate()));
    }


    @PostMapping("/service/exportFactoryCalendarItem")
    public void exportFactoryCalendarItem(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<FactoryCalendarItemEx> factoryCalendarItemPage = factoryCalendarItemService.getFactoryCalendarItemByPage(pageEntity);
        List<FactoryCalendarItemEx> factoryCalendarItemExes = factoryCalendarItemPage.getContent();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("FactoryCalendarItem接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        //EasyExcel.write(response.getOutputStream(), FactoryCalendarItemEx.class).sheet("sheet1").doWrite(factoryCalendarItemExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), FactoryCalendarItemEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(factoryCalendarItemExes);
    }
}